package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.ControlTypeConstant;
import com.tbit.uqbike.client.constant.ForceFinishConstant;
import com.tbit.uqbike.client.constant.RedisConstant;
import com.tbit.uqbike.client.dao.log.ForceFinishDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.MachineBorrow;
import com.tbit.uqbike.client.pojo.newEntity.ForceFinish;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 14:27 2023/4/18
 */
@Service
@Slf4j
public class ForceFinishServiceImpl implements ForceFinishService {

    @Autowired
    private ForceFinishDao forceFinishDao;
    @Autowired
    private FeeAppealService feeAppealService;
    @Autowired
    private MachineBorrowService machineBorrowService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private TerControlService terControlService;
    @Autowired
    private RuleRidelogService ruleRidelogService;

    @Override
    public void insert(String name, ForceFinish forceFinish, HttpServletRequest request) {
        MachineBorrow machineBorrow = machineBorrowService.getByUserId(forceFinish.getUserId());
        if (machineBorrow == null) {
            throw new BaseException("-3006", "不存在借出记录");
        }
        Machine machine = machineService.getByMachineId(machineBorrow.getMachineId());
        forceFinish.setMachineId(machineBorrow.getMachineId());
        forceFinish.setOrderNO(machineBorrow.getOrderNO());
        forceFinish.setImageIds(name);
        forceFinish.setAccountId(machine.getAccountId());
        forceFinish.setAddTime(new Date());
        if (forceFinish.getType() == ForceFinishConstant.FORCEFINISH_TYPE_HELMET) {
            String exData = redisService.get(RedisConstant.FORCE_HELMET_KEY_PREFIX + machine.getMachineNO());
            forceFinish.setExData(exData);
      /*String serNO = UUID.randomUUID().toString();
      String param  = ObjectUtil.buildPhotoParam(1, 2, 2);
      terControlService
          .sendControl(serNO, machine.getMachineNO(), ControlTypeConstant.CONTROL_TYPE_PHOTO,
              param , "", machine, false, 1,
              -1, "头盔还车异常自动下发拍照");
      Integer photoId= ruleRidelogService.insert(machine.getMachineNO(),"头盔还车异常自动下发拍照");
      forceFinish.setPhotoId(photoId);*/
        } else if (forceFinish.getType() == ForceFinishConstant.FORCEFINISH_TYPE_SXT) {
            String serNO = UUID.randomUUID().toString();
            String param = ObjectUtil.buildPhotoParam(1, 2, 2);
            terControlService
                    .sendControl(serNO, machine.getMachineNO(), ControlTypeConstant.CONTROL_TYPE_PHOTO,
                            param, "", machine, false, 1,
                            -1, "摄像头还车异常自动下发拍照");
            Integer photoId = ruleRidelogService.insert(machine.getMachineNO(), "摄像头还车异常自动下发拍照");
            forceFinish.setPhotoId(photoId);
            String exData = redisService.get(RedisConstant.FORCE_SXT_KEY_PREFIX + machine.getMachineNO());
            forceFinish.setExData(exData);
        }
        ForceFinish forceFinishDB = getByOrderNO(forceFinish.getOrderNO());
        if (forceFinishDB != null) {
            edit(forceFinish);
        } else {
            forceFinishDao.insert(forceFinish);
        }
        log.info("强制还车" + forceFinish);
    }

    @Override
    public String uploadFile(MultipartFile file, HttpServletRequest request, Integer accountId) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        String fileName = "";
        /* 判断是否是文件上传 */
        if (multipartResolver.isMultipart(request)) {

            if (file.getSize() > 2 * 1024 * 1024) {
                throw new BaseException("-4003", "图片过大");
            }
            fileName = feeAppealService.ossFileUpload(file, accountId);
        }
        return fileName;
    }


    @Override
    public ForceFinish getByOrderNO(String orderNO) {

        return forceFinishDao.getByOrderNO(orderNO);
    }

    @Override
    public void edit(ForceFinish forceFinish) {
        forceFinishDao.edit(forceFinish);
    }

}
